package com.example.qiandao.controller;

import com.example.qiandao.common.Result;
import com.example.qiandao.controller.requestdto.CheckinHistoryRequestDTO;
import com.example.qiandao.controller.requestdto.CheckinRecordRequestDTO;
import com.example.qiandao.controller.responsedto.CheckinRecordDTO;
import com.example.qiandao.service.CheckinRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkinrecord")
public class CheckinRecordController {

    @Autowired
    private CheckinRecordService checkinRecordService;

    /**
     * 根据 CheckinHistory 的 id 获取所有学生的签到记录
     *
     * @param checkinHistoryId 签到历史记录的 id
     * @return 包含签到记录的 Result 对象
     */
    @GetMapping("/checkinrecordlist/{checkinHistoryId}")
    public Result<List<CheckinRecordDTO>> getCheckinRecordsByHistoryId(@PathVariable Long checkinHistoryId) {
        List<CheckinRecordDTO> checkinRecords = checkinRecordService.getCheckinRecordsByHistoryId(checkinHistoryId);
        if (checkinRecords != null && !checkinRecords.isEmpty()) {
            return Result.success(checkinRecords);
        } else {
            return Result.error(404, "No checkin records found for the given checkin history id");
        }
    }

    /**
     * 根据 userId 和新的签到状态 ID 修改签到记录的状态
     *
     * @param request         学生的 ID
     * @return 包含更新结果的 Result 对象
     */
    @PutMapping("/update")
    public Result<Boolean> updateCheckinState(@RequestBody CheckinRecordRequestDTO request) {
        boolean isUpdated = checkinRecordService.updateCheckinState(request.getUserId(), request.getNewCheckinStateId(), request.getCheckinHistoryId());
        if (isUpdated) {
            return Result.success(true);
        } else {
            return Result.error(404, "Failed to update checkin state");
        }
    }
    //--------------学生部分------------------------------------------------------------------------------------

    //生辰根据学生id和班级id获取全部签到记录

    @GetMapping("/records/{studentId}/{classId}")
    public Result<List<CheckinRecordDTO>> getCheckinRecordByStudentIdAndClassId(
            @PathVariable Long studentId,
            @PathVariable Long classId
    )
        {
        List<CheckinRecordDTO> checkinRecordDTOS = checkinRecordService.getCheckinRecordByStudentIdAndClassId(studentId, classId);
        if (checkinRecordDTOS != null && !checkinRecordDTOS.isEmpty()) {
            return Result.success(checkinRecordDTOS);
        } else {
            return Result.error(404, "No checkin records found for the given student id and class id");
        }
    }
}